<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>角色管理</title>
    <style>
    </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">首页</a><span lay-separator="">/</span>
        <a><cite>角色管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>


<div class="layui-fluid">
    <div class="layui-card">
        <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
        <script type="text/html" id="toolbarDemo">
            <button class="layui-btn layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加角色
            </button>
        </script>
        <script type="text/html" id="table-useradmin-webuser">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>修改</a>
<!--            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
        </script>

    </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form action class="layui-form" lay-filter="component-form-element" id="myform">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称：</label>
            <div class="layui-input-inline">
                <input name="roleName"   required lay-verify="required" autocomplete="off"
                       class="layui-input" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色字码：</label>
            <div class="layui-input-inline">
                <select name="roleCode" id="roleCode" >
                    <option value="admin">管理-admin</option>
                    <option value="finance">财务-finance</option>
                    <option value="agent">代理商-agent</option>
                    <option value="saleMan">业务员-saleMan</option>
                </select>
            </div>
        </div>

        <!--<div class="layui-form-item" id="time">
            <label class="layui-form-label">添加时间：</label>
            <div class="layui-input-inline">
                <input name="createTime" readonly autocomplete="off"
                       class="layui-input" >
            </div>
        </div>-->
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 138px;margin-top: 50px">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
            </div>
        </div>
    </form>
</div>
<script type="text/html" id="role">

</script>
<script type="text/html" id="number">
    {{d.LAY_TABLE_INDEX+1}}
</script>
<%include("/common/foot.html"){}%>
<script>

    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui', 'AliangCommon', 'tree', 'util'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var tree = layui.tree;
        var util = layui.util;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;
        var AliangCommon = layui.AliangCommon;

        var title = "添加角色";
        var findAll_url = 'system/authority/findAll.jhtml';
        var update_url = 'system/authority/save.jhtml';
        var delete_url = 'system/authority/delete.jhtml';

        var index;//定义建立富文本编辑器全局变量
        var layedit;//定义富文本编辑器layedit全局变量
        var cols = [ //表头
            {field:'roleName',sort:'true',title:'角色名称',align:'center'},
            {field:'roleCode',sort:'true',title:'角色代码',align:'center'},
            {field: 'createTime',sort:'true', title: '添加时间',align:'center',templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd ')}}</div>"},
            {title: '操作', fixed: 'right', width: 150, toolbar: '#table-useradmin-webuser',}
        ];
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            findAll_url,
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );
        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('listReload', {
                where: {
                    term: field.search_name
                },
                page: {
                    curr: 1//重新从第 1 页开始
                }
            });
        });

        //常规用法
        laydate.render({
            elem: '#test1'
        });


        //按钮事件
        util.event('lay-demo', {
            getChecked: function (othis) {
                var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
                var ids = getCheckedId(checkedData);

                layer.alert(JSON.stringify(checkedData), {shade: 0});
                console.log(checkedData);
            }
            , setChecked: function () {
                tree.setChecked('demoId1', [1, 2]); //勾选指定节点
            }
            , reload: function () {
                //重载实例
                tree.reload('demoId1', {});

            }
        });


        //初始化修改页面
        function loadData(data) {
            //alert(JSON.stringify(data))
            var ret = {
                "id": data.id,
                "roleName":data.roleName,
                "roleCode":data.roleCode
            };
            form.render();
            return ret;
        }
        // AliangCommon.select({
        //     url: "role/json_role_list.json",
        //     elemIds: 'sysRoleCode',
        //     dataValue: 'id',
        //     dataText: 'role_name',
        //     defaultValue: "role_name",//默认选项（可以是具体某个option的value或text）
        //     otherOption: "<option value=''>选择角色</option>",//额外选项
        //     isLocalRender: true//开启局部渲染
        // });

        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            layer.open({
                type: 1,
                title: title,
                area:['100%', '100%'],
                content: $('#layuiadmin-form-useradmin'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                }
            });
            // layer.full(a);     //全屏显示
            //如果是添加则重置表单，如果是编辑则给表单赋值
            if (data == 0 || data == null) {
                $("#time").hide()
                $('#myform')[0].reset();
            } else {
                layui.form.val("component-form-element", loadData(data));
            }
        }
        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {

             //alert(JSON.stringify(data.field))
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate(0);
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id;//得到复选框的值
                            postDelete(a);
                        }

                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                initUpdate(data);
            } else if (obj.event === 'view') {
                initUpdate1(data);
            } else if (obj.event === 'del') {
                postDelete(data.id);
                // layer.open({
                //     id: 1,
                //     type: 1,
                //     title: '删除企业 敏感操作 请输入二级密码',
                //     skin: 'layui-layer-rim',
                //     area: ['450px', 'auto'],
                //
                //     content: ' <input type="password" name="pwd" required lay-verify="required" placeholder="请输入二级密码" autocomplete="off"\n' +
                //         '               class="layui-input">',
                //     btn: ['保存', '取消'],
                //     btn1: function (index, layero) {
                //         postDelete(data.id);
                //         console.log(layero);
                //         layer.close(index);
                //     },
                //     btn2: function (index, layero) {
                //         layer.close(index);
                //     }
                //
                // });
            }
        });


        //ajax提交删除内容
        window.postDelete = function (id) {
            AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
                if (ret == null) {
                    layer.msg("删除失败", {
                        icon: 5
                    });
                } else {
                    layer.msg("删除成功", {
                        icon: 6
                    });
                    layer.closeAll();
                    table.reload('listReload');
                }
                return ret;
            });
        };

        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data.field));

            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');
                            location.replace(location.href);
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
    });

</script>

</body>

</html>
